package com.yegs.admin.business.controler;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yegs.admin.base.PageVo;
import com.yegs.admin.business.entity.BaseUser;
import com.yegs.admin.business.serviceImpl.BaseRoleServiceImpl;
import com.yegs.admin.business.serviceImpl.BaseUserRoleServiceImpl;
import com.yegs.admin.business.serviceImpl.BaseUserServiceImpl;
import com.yegs.admin.config.JWTUtil;
import com.yegs.admin.config.Msg;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yegs
 * @since 2020-08-22
 */
@RestController
@RequestMapping("/baseUser")
public class BaseUserController{

    @Autowired
    private BaseUserServiceImpl baseUserBaseUserService;

    @Autowired
    private BaseUserRoleServiceImpl baseUserRoleService;

    @Autowired
    private BaseRoleServiceImpl baseRoleServiceImpl;

    @GetMapping("/delbyid")
    @RequiresPermissions("baseUser:delbyid")
    public Msg delById(@RequestParam("id") Long id){
        try {
            baseUserBaseUserService.removeById(id);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",id);
            baseUserRoleService.remove(queryWrapper);
            return Msg.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }
    //保存||更新
    @PostMapping("/save")
    @RequiresPermissions("baseUser:save")
    public Msg save(BaseUser t){
        try {
            baseUserBaseUserService.saveOrUpdate(t);
            return Msg.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    //查询
    @PostMapping("/query")
    @RequiresPermissions("baseUser:query")
    public Msg query(@RequestBody PageVo<BaseUser> pageVo){
        try {
            Page page = new Page(pageVo.getPageNumber(),pageVo.getPageSize());
            QueryWrapper<BaseUser> queryWrapper = new QueryWrapper<BaseUser>();
            queryWrapper.setEntity(pageVo.getEntity());
            page = baseUserBaseUserService.page(page, queryWrapper);
            return Msg.success("page",page);
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }




    @PostMapping("/saveUserAndRoles")
    @RequiresPermissions("baseUser:saveUserAndRoles")
    public Msg saveUserAndRoles(@RequestBody BaseUser baseUser){
        try {
            baseUserBaseUserService.saveUserAndRoles(baseUser);
            return Msg.success("保存成功!");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    @PostMapping("/login")
    public Msg login(@RequestParam(value = "username",required = true) String username,
                     @RequestParam(value = "password",required = true) String password){
        try{
            BaseUser user = new BaseUser();
            user.setUserUsername(username);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_username",username);
            user = baseUserBaseUserService.getOne(wrapper);
            if(user==null){
                return Msg.error("登录用户不存在!");
            }else if(user.getUserPassword().equals(password)){
                return  Msg.success("token",JWTUtil.createToken(username));
            }else {
                return Msg.error("密码错误!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }
    @PostMapping("/logout")
    public Msg login(String token){
        //TODO 清楚redis的token
        return Msg.success("清除token成功");
    }


    @GetMapping("info")
    public Msg info(@Param("token") String token){
        try {
            String username = JWTUtil.getUsername(token);
            HashMap hashMap = new HashMap();
            List<String> roles = baseRoleServiceImpl.findRoleNameByUserName(username);
            String role = "";
            for(String str : roles){
                role += str;
            }
            hashMap.put("name", username);
            hashMap.put("avatar", "");
            hashMap.put("roles",role);
            return  Msg.success(hashMap);
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

}
